//
//  LDJ_RootViewController.m
//  Pianke
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 dada. All rights reserved.
//

#import "LDJ_RootViewController.h"

@interface LDJ_RootViewController ()

@end

@implementation LDJ_RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
        
    }];
    NSMutableArray *array = [NSMutableArray arrayWithObjects:
                             @"welcome_1.png",
                             @"welcome_2.png",
                             @"welcome_3.png",
                             @"welcome_4.png", nil];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * array.count, SCREEN_HEIGHT);
    
    for (int i = 0 ; i < array.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:[array objectAtIndex:i]];
        [self.scrollView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(SCREEN_WIDTH * i);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
            
        }];
    }
    
    // 设置按页滑动
    self.scrollView.pagingEnabled = YES;
    // 取消边界反弹
    self.scrollView.bounces = NO;
    // 是否显示水平、垂直滑动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.delegate = self;
    
    self.pageC = [[UIPageControl alloc] init];
    self.pageC.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.pageC];
    [self.pageC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((SCREEN_WIDTH - 200 * EACH_WIDTH) / 2);
        make.top.mas_equalTo(SCREEN_HEIGHT - 70 * EACH_HEIGHT);
        
        make.size.mas_equalTo(CGSizeMake(200 * EACH_WIDTH, 20 * EACH_HEIGHT));
    }];
    
    // 设置页码个数
    self.pageC.numberOfPages = array.count;
    // 设置当前页码颜色
    self.pageC.currentPageIndicatorTintColor = [UIColor blackColor];
    // 设置未选择的页码颜色
    self.pageC.pageIndicatorTintColor = [UIColor grayColor];
    //  设置当前选中页码
    //    self.pageC.currentPage = 0;
    // 核心方法
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17 * EACH_HEIGHT];
    [btn setTitle:@"「点击进入」" forState:UIControlStateNormal];
    [self.scrollView addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(SCREEN_WIDTH * 3 + 130 * EACH_WIDTH);
        make.top.mas_equalTo(SCREEN_HEIGHT - 55 * EACH_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(120 * EACH_WIDTH, 30 * EACH_HEIGHT));
    
    }];
}

-(void)btnAction: (UIButton *)btn{
    
    
    
    [self.rootViewControllerDelegate changeWindowRootViewController];
    
}

- (void)pageAction: (UIPageControl *)pageC{
    
    [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * pageC.currentPage, 0) animated:YES];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat scro = scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int p = (x + scro / 2) /  scro;
    self.pageC.currentPage = p;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
